package ui;

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.*;

import org.quickconnect.QuickConnect;

public class LockDialog extends JDialog {
	
	JPasswordField pWordInput = null;
	
	public LockDialog() {
		this.setUndecorated(true);
		this.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
		this.setLayout(new FlowLayout());
		final JLabel pWordLabel = new JLabel("Password:");
		final JLabel locked = new JLabel("Server is Locked!");
		Font newLabelFont=new Font(locked.getFont().getName(),Font.BOLD,locked.getFont().getSize()); 
		locked.setFont(newLabelFont);
		pWordInput = new JPasswordField(15);
		JButton ok = new JButton("Unlock");

		JPanel panel1 = new JPanel(new GridLayout(3, 2, 10, 10));
		//		JPanel panel2 = new JPanel();
		//		JPanel panel3 = new JPanel();
		panel1.add(locked);
		panel1.add(Box.createGlue());
		panel1.add(pWordLabel);
		panel1.add(pWordInput);
		panel1.add(Box.createGlue());
		panel1.add(ok);

		this.add(panel1);
		
		ActionListener go = new ActionListener()
		{
			@SuppressWarnings("unchecked")
			public void actionPerformed(ActionEvent e)
			{


				char[] pWord = pWordInput.getPassword();
				String password = "";
				try {
					password = MainFrame.mainFrame.SHA1(new String(pWord));
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				ArrayList unlockParams = new ArrayList();
				HashMap unlockMap = new HashMap();
				unlockMap.put("password", password);
				unlockParams.add(MainFrame.mainFrame);
				unlockParams.add(unlockMap);
				QuickConnect.handleRequest("serverUnlock", unlockParams);
			}
	};
	pWordInput.addActionListener(go);
	ok.addActionListener(go);
	}
	
	public void lockServer() {
		
		pWordInput.setText("");
		this.setModal(true);
		this.pack();
		this.setLocationRelativeTo(MainFrame.mainFrame);
		this.setVisible(true);
	}
	
	public void unlockServer() {
		this.setVisible(false);
	}

}
